package com.esri.core.geometry;

import com.esri.core.internal.geometry.ConversionType;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;

/* loaded from: classes.dex */
public class SpatialReference implements Serializable {
    public static int WKID_WGS84 = 0;
    public static int WKID_WGS84_WEB_MERCATOR = 0;
    public static int WKID_WGS84_WEB_MERCATOR_AUXILIARY_SPHERE = 0;
    public static int WKID_WGS84_WEB_MERCATOR_AUXILIARY_SPHERE_10 = 0;
    private static final long serialVersionUID = 1;
    private int a;
    private String b;
    private Unit c;
    private double d;
    private SpatialReference e;

    static native String[] nativeFromPoints(double[][] dArr, int i, String str, int i2, int i3, int i4, boolean z, boolean z2);

    static native double nativeGetConvergenceAngle(double d, double d2, int i);

    static native int nativeGetCoordinateSystemType(int i);

    static native int nativeGetCoordinateSystemTypeFromWKT(String str);

    static native int nativeGetGCS(int i);

    static native int nativeGetLatestID(int i);

    static native int nativeGetLatestIDFromWKT(String str);

    static native int nativeGetOldID(int i);

    static native int nativeGetOldIDFromWKT(String str);

    static native double[] nativeGetPannableExtent(int i);

    static native double nativeGetTolerance(int i, int i2);

    static native int nativeGetUnit(int i);

    static native int nativeGetWKID(String str);

    static native String nativeGetWKT(int i);

    static native double[][] nativeToPoints(String[] strArr, int i, String str, int i2, int i3);

    static {
        GeometryEngine.a();
        WKID_WGS84 = 4326;
        WKID_WGS84_WEB_MERCATOR = 102113;
        WKID_WGS84_WEB_MERCATOR_AUXILIARY_SPHERE = 102100;
        WKID_WGS84_WEB_MERCATOR_AUXILIARY_SPHERE_10 = 3857;
    }

    /* loaded from: classes.dex */
    public enum Type {
        LOCAL(0),
        GEOGRAPHIC(1),
        PROJECTED(2);
        
        private int a;

        Type(int i) {
            this.a = i;
        }

        public static Type fromCode(int i) {
            if (i != 0) {
                if (i != 1) {
                    if (i == 2) {
                        return PROJECTED;
                    }
                    return LOCAL;
                }
                return GEOGRAPHIC;
            }
            return LOCAL;
        }

        public static int toCode(Type type) {
            return type.a;
        }
    }

    protected SpatialReference() {
        this.a = -1;
        this.b = null;
        this.c = null;
        this.d = Double.NaN;
        this.e = null;
    }

    protected SpatialReference(int i) {
        this.a = -1;
        this.b = null;
        this.c = null;
        this.d = Double.NaN;
        this.e = null;
        this.a = i;
    }

    private SpatialReference(String str) {
        this.a = -1;
        this.b = null;
        this.c = null;
        this.d = Double.NaN;
        this.e = null;
        this.b = str;
    }

    public static SpatialReference create(int i) {
        return new SpatialReference(i);
    }

    public static SpatialReference create(String str) {
        SpatialReference spatialReference = new SpatialReference(str);
        spatialReference.a = nativeGetWKID(str);
        return spatialReference;
    }

    public static SpatialReference createLocal(Unit unit) {
        SpatialReference spatialReference = new SpatialReference();
        spatialReference.c = unit;
        return spatialReference;
    }

    public static SpatialReference createLocal() {
        return new SpatialReference();
    }

    public static SpatialReference fromJson(JsonParser jsonParser) throws Exception {
        if (InternalUtils.a(jsonParser)) {
            int i = 0;
            String str = null;
            while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                String currentName = jsonParser.getCurrentName();
                jsonParser.nextToken();
                if (jsonParser.getCurrentToken() != JsonToken.VALUE_NULL) {
                    if ("wkid".equals(currentName)) {
                        i = jsonParser.getIntValue();
                    } else if ("wkt".equals(currentName)) {
                        str = jsonParser.getText();
                    } else {
                        jsonParser.skipChildren();
                    }
                }
            }
            if (i > 0) {
                try {
                    return create(i);
                } catch (IllegalArgumentException e) {
                    if (str == null || str.length() == 0) {
                        throw e;
                    }
                }
            }
            if (str == null || str.length() == 0) {
                return null;
            }
            return create(str);
        }
        return null;
    }

    public String toJson() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        if (this.a > 0) {
            sb.append("\"wkid\":" + this.a);
        }
        sb.append("}");
        return sb.toString();
    }

    public Type getCoordinateSystemType() {
        int i = this.a;
        if (i == 0) {
            return Type.fromCode(nativeGetCoordinateSystemTypeFromWKT(this.b));
        }
        return Type.fromCode(nativeGetCoordinateSystemType(i));
    }

    public int getID() {
        return this.a;
    }

    public int getLatestID() {
        int i = this.a;
        if (i == 0) {
            return nativeGetLatestIDFromWKT(this.b);
        }
        if (i == -1) {
            return -1;
        }
        return nativeGetLatestID(i);
    }

    public int getOldID() {
        int i = this.a;
        if (i == 0) {
            return nativeGetOldIDFromWKT(this.b);
        }
        if (i == -1) {
            return -1;
        }
        return nativeGetOldID(i);
    }

    public String getText() {
        String str = this.b;
        if (str == null && this.a == -1) {
            return null;
        }
        if (str == null) {
            this.b = nativeGetWKT(this.a);
        }
        return this.b;
    }

    public Unit getUnit() {
        if (this.c == null) {
            int i = this.a;
            if (i > 0) {
                this.c = Unit.create(nativeGetUnit(i));
            } else {
                String str = this.b;
                if (str != null) {
                    this.c = Unit.a(str);
                }
            }
        }
        return this.c;
    }

    double a(int i) {
        return nativeGetTolerance(this.a, i);
    }

    public double getTolerance() {
        if (this.d == Double.NaN) {
            this.d = a(0);
        }
        return this.d;
    }

    public SpatialReference getGCS() {
        if (this.e == null) {
            this.e = new SpatialReference(nativeGetGCS(this.a));
        }
        return this.e;
    }

    public Envelope getPannableExtent() {
        double[] nativeGetPannableExtent;
        int i = this.a;
        if (i == -1 || (nativeGetPannableExtent = nativeGetPannableExtent(i)) == null || nativeGetPannableExtent.length != 4) {
            return null;
        }
        return new Envelope(nativeGetPannableExtent[0], nativeGetPannableExtent[1], nativeGetPannableExtent[2], nativeGetPannableExtent[3]);
    }

    public boolean isWGS84() {
        return this.a == WKID_WGS84;
    }

    public boolean isAnyWebMercator() {
        int i = this.a;
        return i == WKID_WGS84_WEB_MERCATOR || i == WKID_WGS84_WEB_MERCATOR_AUXILIARY_SPHERE || i == WKID_WGS84_WEB_MERCATOR_AUXILIARY_SPHERE_10;
    }

    Object writeReplace() throws ObjectStreamException {
        SpatialReferenceSerializer spatialReferenceSerializer = new SpatialReferenceSerializer();
        spatialReferenceSerializer.setSpatialReferenceByValue(this);
        return spatialReferenceSerializer;
    }

    public boolean equals(Object obj) {
        if (obj != null && (obj instanceof SpatialReference)) {
            SpatialReference spatialReference = (SpatialReference) obj;
            int id = spatialReference.getID();
            if (getID() == id) {
                return true;
            }
            if (getID() > 0 || id > 0) {
                if (isAnyWebMercator() && spatialReference.isAnyWebMercator()) {
                    return true;
                }
                if (getID() > 0 && getID() > 0) {
                    return false;
                }
            }
            String text = spatialReference.getText();
            if (text == null || getText() == null) {
                return false;
            }
            return getText().toLowerCase().equals(text.toLowerCase());
        }
        return false;
    }

    public String toString() {
        return "SpatialReference [mWKID=" + this.a + ", mWKT=" + this.b + ", mUnit=" + this.c + ", mTolerance=" + this.d + ", mGCS=" + this.e + "]";
    }

    public double getConvergenceAngle(Point point) {
        return nativeGetConvergenceAngle(point.getX(), point.getY(), this.a);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public static List<Point> a(List<String> list, SpatialReference spatialReference, ConversionType conversionType, int i) {
        if (list == null || spatialReference == null) {
            throw new RuntimeException("Conversion of coordinates failed. Input coordinates and/or spatial reference is null.");
        }
        return a(nativeToPoints((String[]) list.toArray(new String[0]), spatialReference.getID(), spatialReference.getText(), conversionType.getCode(), i));
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public static List<String> a(List<Point> list, SpatialReference spatialReference, ConversionType conversionType, int i, int i2, boolean z, boolean z2) {
        if (list == null || spatialReference == null) {
            throw new RuntimeException("Conversion of points failed. Input points and/or spatial reference is null.");
        }
        return Arrays.asList(nativeFromPoints(a(list), spatialReference.getID(), spatialReference.getText(), conversionType.getCode(), i, i2, z, z2));
    }

    private static List<Point> a(double[][] dArr) {
        if (dArr == null) {
            return null;
        }
        LinkedList linkedList = new LinkedList();
        for (int i = 0; i < dArr.length; i++) {
            linkedList.add(new Point(dArr[i][0], dArr[i][1]));
        }
        return linkedList;
    }

    private static double[][] a(List<Point> list) {
        if (list == null) {
            return null;
        }
        double[][] dArr = (double[][]) Array.newInstance(double.class, list.size(), 2);
        int i = 0;
        for (Point point : list) {
            if (point == null) {
                throw new RuntimeException("Conversion of one or more points failed. One of the input points is null.");
            }
            dArr[i][0] = point.getX();
            dArr[i][1] = point.getY();
            i++;
        }
        return dArr;
    }
}
